package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 班级管理
 */
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Resource
    private ClazzService clazzService;

    //分页查询班级列表
    @GetMapping
    public Result clazzQuery(String name,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询员工列表，page={}, pageSize={}, name={}, " +
                        "begin={}, end={}",
                page, pageSize, name, begin, end);

        PageBean<Clazz> pageBean = clazzService.pageQuery(name,begin,end,page,pageSize);
        return Result.success(pageBean);
    }

    //删除班级
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        //log.info("删除班级，id={}",id);
        clazzService.deleteById(id);
        return Result.success();

    }

    //新增班级
    @PostMapping
//    @JsonFormat(pattern = "yyyy-MM-dd")
    public Result add(@RequestBody Clazz clazz){
//        log.info("删除班级，clazz={}",clazz);
        clazzService.addClazz(clazz);
        return Result.success();
    }

    /**
     * 根据ID查询班级接口
     */
    @GetMapping("/{id}")
    public Result selectByID(@PathVariable("id") Integer id){
        Clazz clazzData = clazzService.selectById(id);
        return Result.success(clazzData);
    }

    /**
     * 修改班级
     */
    @PutMapping
    public Result putClazz(@RequestBody Clazz clazz){
        //log.info("clazz={}",clazz);
        clazzService.putClazz(clazz);
        return Result.success();
    }

    /**
     * 查询所有班级
     */
    @GetMapping("list")
    public Result getAllClazz(){
        List<Clazz> lists =  clazzService.getAllClazz();
        return Result.success(lists);
    }
}
